package com.situ.ymall.config;


import com.situ.ymall.intercepter.CleanOrderIntercepter;
import com.situ.ymall.intercepter.UserIntercepter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//相当于springmvc 的配置文件
 @Configuration
public class WebConfig implements WebMvcConfigurer {


     @Autowired
     private UserIntercepter userIntercepter;

     @Value("${spring.servlet.multipart.location}")
     private String path;

     @Autowired
     private CleanOrderIntercepter cleanOrderIntercepter;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //配置无业务逻辑的路径与视图的映射
        registry.addViewController("/").setViewName("index");

    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/upload/**").addResourceLocations("file:"+path);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userIntercepter).
                addPathPatterns("/user/**").    // 拦截路径
                excludePathPatterns("/user/login","/user/reg");     // 方形路径
        registry.addInterceptor(cleanOrderIntercepter)
                .addPathPatterns("/goods","/user/cart/add","/api/cart/**","/user/order/add", "/api/order/getList");
    }
}
